/**
 * @author liujing 121250083
 * last changing time:2013.4.28 0:43-1:10;
 * This class is used to help me learn about the arraylist.
 * I copy and paste almost the methods listed in the implementation comment in javaAPI and do the test.
 * Pay attention that this is just a test class for me to get used to the usage of arraylist so I declare new MyList
 * in the inner part but not fully at the beginning of a block,which  not matches the required coding style.
 * However ,in this kind of class ,I think it is Okay.
 */
package arraylist;

import java.util.*;

public class MyList extends ArrayList {
	/**
	 * @version 2.0 28th April 2013
	 * @param args
	 */
	public static void main(String[] args) {
		MyList myList = new MyList();
		Iterator iterator = myList.iterator();
		/* add(int index, E element) */
		myList.add('a');
		myList.add('b');
		myList.add('c');
		myList.add('d');
		myList.add('e');
		System.out.println("add");
		myList.goThrough3();
		/*
		 * addAll(Collection<? extends E> c) addAll(int index,
		 * Collection<?extends E> c)
		 */
		System.out.println("addAll ");
		MyList list2 = new MyList();
		for (int i = 0; i < 3; i++) {
			list2.add(i * 2);
		}
		myList.addAll(list2);
		myList.goThrough3();
		for (int i = 0; i < 3; i++) {
			list2.set(i, i * 2 + 1);
		}
		myList.addAll(1, list2);
		myList.goThrough1();
		/* contains(Object o) */
		System.out.println("contains");
		System.out.println(myList.contains('a'));
		System.out.println(myList.contains(0));
		/*
		 * remove(int index) remove(Object o)
		 */
		System.out.println("remove");
		myList.remove(0);
		myList.goThrough1();

		myList.remove(Integer.valueOf('1'));
		myList.goThrough1();
		/* removeRange(int fromIndex, int toIndex) */
		System.out.println("removeRange");
		myList.removeRange(3, 6);
		myList.goThrough2();
		/* clone() Returns a shallow copy of this ArrayList instance. */
		System.out.println("clone");
		list2 = (MyList) myList.clone();
		list2.goThrough1();
		/* clear() Removes all of the elements from this list. */
		System.out.println("clear");
		myList.clear();
		myList.goThrough1();
		/* isEmpty() Returns true if this list contains no elements. */
		System.out.println("isEmpty");
		System.out.println(" " + myList.isEmpty());
		/*
		 * trimToSize()Trims the capacity of this ArrayList instance to be the
		 * list's current size.
		 */

		myList.trimToSize();

		/* removeAll(Collection) */
		System.out.println("remove all");
		list2.goThrough1();
		MyList list3 = new MyList();
		for (int i = 0; i < 3; i++) {
			list3.add(i * 2);
		}
		list2.removeAll(list3);
		list2.goThrough2();

	}

	/*
	 * use for(,,)to go through an arraylist
	 */
	public void goThrough1() {
		for (int i = 0; i < this.size(); i++) {
			System.out.print(" " + this.get(i));
		}
		System.out.println();
	}

	/*
	 * use foreach to go through an arraylist
	 */
	public void goThrough2() {
		for (Object element : this) {
			System.out.print(" " + element);
		}
		System.out.println();
	}

	/*
	 * use iterator to go through an arraylist
	 */
	public void goThrough3() {
		Iterator iterator = this.iterator();
		while (iterator.hasNext()) {
			System.out.print(" " + iterator.next());
		}
		System.out.println();
	}

}
